package com.cmcc.passenger.ui.profile


import android.app.Dialog
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.alibaba.android.arouter.facade.annotation.Route
import com.cmcc.passenger.R
import com.cmcc.passenger.config.PassPath
import com.cmcc.passenger.data.entity.request.EvaluateRequest
import com.cmcc.passenger.databinding.ActivityCommentBinding
import com.cmcc.passenger.ui.base.BasicActivity
import com.cmcc.passenger.ui.fragment.AppAlertDialog
import com.cmcc.passenger.ui.fragment.AppProgressDialog
import com.cmcc.passenger.ui.order.viewmodel.OrderDetailViewModel
import com.cmcc.shared.utils.PassDialogUtils
import kotlinx.android.synthetic.main.layout_comment_input.view.*


/**
 * 司机加盟
 */
@Route(path = PassPath.COMMENT)
class
CommentActivity : BasicActivity() {
    private lateinit var binding: ActivityCommentBinding

    private var progressDialog: AppProgressDialog? = null


    private lateinit var viewModel: OrderDetailViewModel

    private lateinit var drivePhone: String

    private var score: Double = 0.0

    private var dialog: Dialog? = null
    /**
     * 初始化
     */
    override fun initialize(savedInstanceState: Bundle?) {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_comment)

        progressDialog = AppProgressDialog(this)
        viewModel = ViewModelProviders.of(this).get(OrderDetailViewModel::class.java)


        binding.appbar.title.text = getString(R.string.driver_finish)

        binding.appbar.toolbar.setNavigationOnClickListener { finish() }

        initBgr()

        /**
         * 订单详情数据 监听
         */
        viewModel.orderDetail.observe(this, Observer { orderDetail ->
            if (orderDetail != null) {
                //个人信息
                binding.person.tvName.text = orderDetail.driver.name
                binding.person.tvCardNo.text = orderDetail.driverCarIdCard
                binding.person.tvScore.text = orderDetail.score
                drivePhone = orderDetail.driver.phone
                binding.person.avatarUrl = orderDetail.driver.headPortraitAddress
                //订单信息
                binding.pay.tvMoney.text =
                    orderDetail.bountyFee.toDouble().let {
                        orderDetail.totalAmount.toDouble().plus(
                            it
                        )
                    }.toString()
                binding.pay.tvOrderNo.text =
                    resources.getString(R.string.order_number_title) + "" + orderDetail.orderNumber

            }

            binding.person.llCall.setOnClickListener(View.OnClickListener {
                if (TextUtils.isEmpty(drivePhone)) {
                    return@OnClickListener
                }
                AppAlertDialog(this).setContent(getString(R.string.pass_if_you_need_call))
                    .setNavigation(getString(R.string.cancel), null)
                    .setPositive(getString(R.string.make_sure),
                        View.OnClickListener {


                            val data = Uri.parse("tel:" + drivePhone)
                            val intent = Intent(Intent.ACTION_DIAL, data)
                            startActivity(intent)

                        }).show()

            })
            //帮助
            binding.person.llHelp.setOnClickListener(View.OnClickListener {
                AppAlertDialog(this).setContent(getString(R.string.pass_if_you_need_call))
                    .setNavigation(getString(R.string.cancel), null)
                    .setPositive(getString(R.string.make_sure),
                        View.OnClickListener {


                            val data = Uri.parse("tel:85262325253")
                            val intent = Intent(Intent.ACTION_DIAL, data)
                            startActivity(intent)

                        }).show()
            })
            //回到主页
            binding.person.llMain.setOnClickListener(View.OnClickListener {

                finish()

            })

        })

        val id = intent.getStringExtra("order_id")
        if (id.isNotEmpty()) {
            //获取订单详情
            viewModel.getOrderDetail(id)
        }
        binding.tvOk.setOnClickListener(View.OnClickListener {
            progressDialog!!.show()
            var request = EvaluateRequest(id, score, binding.input.etCommentContent.text.toString())
            viewModel.evaluate(request)

        })


        /**
         * 订单详情数据 监听
         */
        viewModel.evaluateData.observe(this, Observer { evaluateData ->
            progressDialog!!.dismiss()
            if (evaluateData != null && evaluateData.code == 200) {
                dialog =
                    PassDialogUtils.getDialog(
                        this,
                        R.layout.dialog_commit_success
                    )
                dialog?.show()

            } else {
                Toast.makeText(
                    this,
                    evaluateData.msg,
                    Toast.LENGTH_LONG
                ).show()
            }
        })

        viewModel.finishEven.observe(this, Observer {
            if (it) finish()
        })

    }

    private fun initBgr() {
        binding.setting.ivOne.setOnClickListener(View.OnClickListener {
            score = 1.0
            binding.setting.ivOne.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivTwo.setImageResource(R.drawable.image_no_comment)
            binding.setting.ivThree.setImageResource(R.drawable.image_no_comment)
            binding.setting.ivFour.setImageResource(R.drawable.image_no_comment)
            binding.setting.ivFive.setImageResource(R.drawable.image_no_comment)
        })
        binding.setting.ivTwo.setOnClickListener(View.OnClickListener {
            score = 2.0
            binding.setting.ivOne.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivTwo.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivThree.setImageResource(R.drawable.image_no_comment)
            binding.setting.ivFour.setImageResource(R.drawable.image_no_comment)
            binding.setting.ivFive.setImageResource(R.drawable.image_no_comment)
        })

        binding.setting.ivThree.setOnClickListener(View.OnClickListener {
            score = 3.0
            binding.setting.ivOne.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivTwo.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivThree.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivFour.setImageResource(R.drawable.image_no_comment)
            binding.setting.ivFive.setImageResource(R.drawable.image_no_comment)
        })
        binding.setting.ivFour.setOnClickListener(View.OnClickListener {
            score = 4.0
            binding.setting.ivOne.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivTwo.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivThree.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivFour.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivFive.setImageResource(R.drawable.image_no_comment)
        })
        binding.setting.ivFive.setOnClickListener(View.OnClickListener {
            score = 5.0
            binding.setting.ivOne.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivTwo.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivThree.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivFour.setImageResource(R.drawable.image_has_comment)
            binding.setting.ivFive.setImageResource(R.drawable.image_has_comment)
        })
    }


}